﻿Imports System.Math
Imports System.Drawing
Imports System.Windows.Forms
Imports System.IO

Public Class Form6

    Dim image1 As Bitmap
    Dim imageSize
    Private Buffer_Block(7, 7) As Byte
    Private quantization_matrix(7, 7) As Byte
    Private DCT_Block(7, 7) As Long
    Dim masterImageArray As New List(Of BlockChars)
    Dim savedFileName As String
    Private Const PI = 3.14159265358979

    Private Sub Form4_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    End Sub

    Private Sub btnBrowseImage_Click(sender As System.Object, e As System.EventArgs) Handles btnBrowseImage.Click
        masterImageArray = New List(Of BlockChars)
        Buffer_Block(7, 7) = New Byte
        DCT_Block(7, 7) = New Long
        ofdLoadImage.Title = "Please Select a File"
        ofdLoadImage.InitialDirectory = "C:temp"
        ofdLoadImage.ShowDialog()
    End Sub

    Private Sub ofdLoadImage_FileOk(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles ofdLoadImage.FileOk
        ofdLoadImage.OpenFile()
        tbImage.Text = ofdLoadImage.FileName.ToString()
        image1 = Image.FromFile(tbImage.Text)
    End Sub

    Sub CalculateDCT(imageToCalculate As Bitmap, imgArray As List(Of BlockChars))
        imageSize = imageToCalculate.Size
        Dim numberofBlocks = (imageSize.Width / 8) * (imageSize.Height / 8)
        Dim blockCounter As Integer = 1

        For j = 0 To (imageSize.height / 8) - 1
            For i = 0 To (imageSize.width / 8) - 1
                Dim xctr As Integer = 0
                For xx = 0 + (i * 8) To 7 + (i * 8)
                    Dim yctr As Integer = 0
                    For yy = 0 + (j * 8) To 7 + (j * 8)
                        Buffer_Block(xctr, yctr) = (0.299 * imageToCalculate.GetPixel(xx, yy).R) + (0.5815 * imageToCalculate.GetPixel(xx, yy).G) + (0.114 * imageToCalculate.GetPixel(xx, yy).B)
                        yctr += 1
                    Next
                    xctr += 1
                Next

                Dim Temp As Double = 0
                Dim U As Integer, V As Integer, X As Integer, Y As Integer = 0

                For U = 0 To 7
                    For V = 0 To 7
                        Temp = 0

                        For X = 0 To 7
                            For Y = 0 To 7
                                Temp = Temp + (Buffer_Block(X, Y)) * _
               Cos((((2 * X) + 1) * U * PI) / 16) * _
               Cos((((2 * Y) + 1) * V * PI) / 16)

                            Next Y
                        Next X

                        'DCT_Block(U, V) = Rnd((C(U) * C(V) * Temp / 4) / quantization_matrix(U, V))
                        DCT_Block(U, V) = (C(U) * C(V) * Temp / 4)
                    Next V
                Next U

                imgArray.Add(New BlockChars(blockCounter, Print_DCT(), j, i))
                blockCounter += 1
            Next
        Next
    End Sub

    Private Function Print_DCT() As Integer
        Dim I As Integer, J As Integer = 0
        Dim totalDCT As Integer = 0
        'For I = 0 To 3
        '    For J = 0 To 3
        '        If DCT_Block(J, I) <> 0 Then
        '            totalDCT += 1
        '        End If

        '    Next J
        'Next I
        'Return totalDCT


        For I = 0 To 3
            For J = 0 To 3
                If Not (J = 0 AndAlso I = 0) Then
                    totalDCT += If(DCT_Block(J, I) < 0, DCT_Block(J, I) * -1, DCT_Block(J, I))
                End If
            Next J
        Next I


        Return totalDCT / 15
    End Function


    Private Function C(Num As Integer) As Double
        If Num = 0 Then
            C = 1 / Math.Sqrt(2)
        Else
            C = 1
        End If
        Return C
    End Function


    Structure BlockChars
        Public blockNumber As Integer
        Public dctValue As Integer
        Public xValue As Integer
        Public yValue As Integer
        Public Sub New(blockNumber As Integer, dctValue As Integer, xValue As Integer, yValue As Integer)
            Me.blockNumber = blockNumber
            Me.xValue = xValue
            Me.yValue = yValue
            Me.dctValue = dctValue
        End Sub
    End Structure

    Private Sub btnScanImage_Click(sender As System.Object, e As System.EventArgs) Handles btnScanImage.Click
        CalculateDCT(image1, masterImageArray)
        processBlockDifferences()
    End Sub

    Sub processBlockDifferences()

        Dim tifimage As Image = Image.FromFile(tbImage.Text) 'indexed format image.
        Dim NonIndexedImage As New Bitmap(tifimage.Width, tifimage.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb)
        SafeDispose(CType(Me.PictureBox1.Image, IDisposable))
        Dim g As Graphics = Graphics.FromImage(CType(NonIndexedImage, Image))

        g.DrawImage(tifimage, 0, 0, tifimage.Width, tifimage.Height)
        g.Dispose()
        tifimage.Dispose()


        Dim ctrBlock As Integer = masterImageArray.Count - 1
        Dim horizontalLineIndicatorCtr As Integer = 0
        Dim verticalLineIndicatorCtr As Integer = 0
        Dim arrayOfYCoordinates As New ArrayList
        Dim arrayOfXCoordinates As New ArrayList
        Dim yValue As Integer = Nothing
        Dim xValue As Integer = Nothing
        'For ctr = 0 To ctrBlock
        '    If masterImageArray(ctr).dctValue >= TextBoxDCTCtr.Text Then
        '        'If xValue = masterImageArray(ctr).yValue Then
        '        '    verticalLineIndicatorCtr += 1
        '        '    If verticalLineIndicatorCtr = 12 Then
        '        '        arrayOfXCoordinates.Add(xValue)
        '        '    End If
        '        'End If
        '        If yValue + 1 = masterImageArray(ctr).yValue Then
        '            If horizontalLineIndicatorCtr = 15 Then
        '                If Not arrayOfYCoordinates.Contains(masterImageArray(ctr).xValue) Then
        '                    arrayOfYCoordinates.Add(masterImageArray(ctr).xValue)
        '                End If
        '                horizontalLineIndicatorCtr = 0
        '            End If
        '            horizontalLineIndicatorCtr += 1
        '        End If
        '        'xValue = masterImageArray(ctr).xValue
        '        yValue = masterImageArray(ctr).yValue
        '    Else
        '        horizontalLineIndicatorCtr = 0
        '    End If
        'Next

        Dim isMinumumYLines As Boolean = True
        Dim numberofVBlocks As Integer = imageSize.height / 8
        Dim horizontalCtr As Integer = 45
        While isMinumumYLines
            For ctr = 0 To numberofVBlocks - 1
                Dim checkBlock = From i In masterImageArray
                                 Where i.xValue = ctr
                For Each cb In checkBlock
                    If cb.dctValue >= TextBoxDCTCtr.Text Then
                        horizontalLineIndicatorCtr += 1
                        If horizontalLineIndicatorCtr = horizontalCtr Then
                            If Not arrayOfYCoordinates.Contains(cb.xValue) Then
                                arrayOfYCoordinates.Add(cb.xValue)
                            End If
                        End If
                    End If
                Next
                horizontalLineIndicatorCtr = 0
            Next
            If arrayOfYCoordinates.Count > 5 Then
                isMinumumYLines = False
            Else
                horizontalCtr -= 1
            End If
        End While

        arrayOfYCoordinates.Sort()
        Dim firstYAxis As Integer
        Dim secondYAxis As Integer
        Dim lastDiffY As Integer
        For i As Integer = 0 To arrayOfYCoordinates.Count - 1
            firstYAxis = arrayOfYCoordinates(i)
            If i + 1 <= arrayOfYCoordinates.Count - 1 Then
                secondYAxis = arrayOfYCoordinates(i + 1)
                If secondYAxis - firstYAxis > 1 Then
                    While secondYAxis - firstYAxis > lastDiffY And Not lastDiffY <= 10
                        arrayOfYCoordinates.Add(firstYAxis + lastDiffY)
                        firstYAxis += lastDiffY
                    End While
                    lastDiffY = secondYAxis - firstYAxis
                End If
            End If
        Next


        Dim numberofHBlocks As Integer = imageSize.Width / 8
        For ctr = 0 To numberofHBlocks - 1
            Dim checkBlock = From i In masterImageArray
                             Where i.yValue = ctr
            For Each cb In checkBlock
                If cb.dctValue >= TextBoxDCTCtr.Text Then
                    verticalLineIndicatorCtr += 1
                    If verticalLineIndicatorCtr = 25 Then
                        If Not arrayOfXCoordinates.Contains(cb.yValue) Then
                            arrayOfXCoordinates.Add(cb.yValue)
                        End If
                    End If
                End If
            Next
            verticalLineIndicatorCtr = 0
        Next

        Dim firstXAxis As Integer
        Dim secondXAxis As Integer
        Dim lastDiffX As Integer
        For i As Integer = 0 To arrayOfXCoordinates.Count - 1
            firstXAxis = arrayOfXCoordinates(i)
            If i + 1 <= arrayOfXCoordinates.Count - 1 Then
                secondXAxis = arrayOfXCoordinates(i + 1)
                If secondXAxis - firstXAxis > 1 Then
                    If secondXAxis - firstXAxis > lastDiffX And Not lastDiffX = 0 Then
                        arrayOfXCoordinates.Add(firstXAxis + lastDiffX)
                    End If
                    lastDiffX = secondXAxis - firstXAxis
                End If
            End If
        Next




        'Dim firstVertLine As Integer = 0
        'Dim LastVertLine As Integer = 0
        'For itemV = 0 To arrayOfXCoordinates.Count - 1
        '    If itemV = 0 Then
        '        firstVertLine = arrayOfXCoordinates(itemV)
        '    End If
        '    LastVertLine = arrayOfXCoordinates(itemV)
        'Next

        'Dim waferWidth As Integer = LastVertLine - firstVertLine
        'arrayOfXCoordinates.Clear()
        'Dim nextVertLine As Integer = firstVertLine
        'arrayOfXCoordinates.Add(nextVertLine)
        'For i = 1 To 16
        '    nextVertLine += CInt(Round(waferWidth / 16))
        '    arrayOfXCoordinates.Add(nextVertLine)
        'Next

        'Dim firstHorLine As Integer = 0
        'Dim LastHorLine As Integer = 0
        'For itemH = 0 To arrayOfYCoordinates.Count - 1
        '    If itemH = 3 Then
        '        firstHorLine = arrayOfYCoordinates(itemH)
        '    End If
        '    LastHorLine = arrayOfYCoordinates(itemH)
        'Next

        'Dim waferHeight As Integer = LastHorLine - firstHorLine
        'arrayOfYCoordinates.Clear()
        'Dim nexthorLine As Integer = firstHorLine
        'arrayOfYCoordinates.Add(nexthorLine)
        'For i = 1 To 8
        '    nexthorLine += CInt(Round(waferHeight / 8))
        '    arrayOfYCoordinates.Add(nexthorLine)
        'Next
        arrayOfXCoordinates.Sort()
        arrayOfYCoordinates.Sort()
        For yCoordCtr = 0 To arrayOfYCoordinates.Count - 2
            If arrayOfYCoordinates(yCoordCtr + 1) - arrayOfYCoordinates(yCoordCtr) > 5 Then
                For xCoordCtr = 0 To arrayOfXCoordinates.Count - 2
                    If arrayOfXCoordinates(xCoordCtr + 1) - arrayOfXCoordinates(xCoordCtr) > 5 Then


                        Dim innerActiveblock = From ab In masterImageArray
                                          Where (ab.xValue > arrayOfYCoordinates(yCoordCtr) + 1 AndAlso ab.xValue < arrayOfYCoordinates(yCoordCtr + 1) - 1) AndAlso
                                          (ab.yValue > arrayOfXCoordinates(xCoordCtr) + 1 AndAlso ab.yValue < arrayOfXCoordinates(xCoordCtr + 1) - 1)
                        For Each item In innerActiveblock

                            If item.dctValue >= TextBoxDCTCtr.Text Then
                                xValue = item.yValue
                                yValue = item.xValue

                                Dim xLine As Integer = 0
                                For xx = 0 + (xValue * 8) To 7 + (xValue * 8)
                                    Dim yLine As Integer = 0
                                    For yy = 0 + (yValue * 8) To 7 + (yValue * 8)
                                        If (xLine = 0 OrElse xLine = 7) OrElse (yLine = 0 OrElse yLine = 7) Then
                                            NonIndexedImage.SetPixel(xx, yy, Color.Red) ' Color.FromArgb(rc, gc, bc))                                    
                                        End If
                                        yLine += 1
                                    Next
                                    Me.PictureBox1.Image = NonIndexedImage
                                    Me.PictureBox1.Refresh()
                                    xLine += 1
                                Next


                                Dim activeBlock = From ab In masterImageArray
                                  Where _
                                  ((ab.xValue = arrayOfYCoordinates(yCoordCtr) Or ab.xValue = arrayOfYCoordinates(yCoordCtr + 1)) AndAlso
                                 (ab.yValue > arrayOfXCoordinates(xCoordCtr) AndAlso ab.yValue <= arrayOfXCoordinates(xCoordCtr + 1))) _
                                  Or _
                                   ((ab.xValue > arrayOfYCoordinates(yCoordCtr) AndAlso ab.xValue <= arrayOfYCoordinates(yCoordCtr + 1)) AndAlso
                                          (ab.yValue = arrayOfXCoordinates(xCoordCtr) Or ab.yValue = arrayOfXCoordinates(xCoordCtr + 1)))


                                For Each itemx In activeBlock
                                    xValue = itemx.yValue
                                    yValue = itemx.xValue

                                    Dim xxLine As Integer = 0
                                    For xx = 0 + (xValue * 8) To 7 + (xValue * 8)
                                        Dim yyLine As Integer = 0
                                        For yy = 0 + (yValue * 8) To 7 + (yValue * 8)
                                            If (xxLine = 0 OrElse xxLine = 7) OrElse (yyLine = 0 OrElse yyLine = 7) Then
                                                NonIndexedImage.SetPixel(xx, yy, Color.Yellow) ' Color.FromArgb(rc, gc, bc))                                    
                                            End If
                                            yyLine += 1
                                        Next
                                        Me.PictureBox1.Image = NonIndexedImage
                                        Me.PictureBox1.Refresh()

                                        xxLine += 1
                                    Next

                                Next
                            End If
                        Next
                    End If
                Next
                'Dim activeBlock = From ab In masterImageArray
                '  Where ((ab.xValue > arrayOfYCoordinates(yCoordCtr) AndAlso ab.xValue < arrayOfYCoordinates(yCoordCtr + 1)) AndAlso
                '         (ab.yValue = arrayOfXCoordinates(xCoordCtr) + 1 Or ab.yValue = arrayOfXCoordinates(xCoordCtr + 1))) Or
                '((ab.xValue = arrayOfYCoordinates(yCoordCtr) + 1 Or ab.xValue = arrayOfYCoordinates(yCoordCtr + 1)) AndAlso
                ' (ab.yValue > arrayOfXCoordinates(xCoordCtr) AndAlso ab.yValue < arrayOfXCoordinates(xCoordCtr + 1)))

                'For Each item In activeBlock
                '    xValue = item.yValue
                '    yValue = item.xValue - 1

                '    Dim xLine As Integer = 0
                '    For xx = 0 + (xValue * 8) To 7 + (xValue * 8)
                '        Dim yLine As Integer = 0
                '        For yy = 0 + (yValue * 8) To 7 + (yValue * 8)
                '            If (xLine = 0 OrElse xLine = 7) OrElse (yLine = 0 OrElse yLine = 7) Then
                '                NonIndexedImage.SetPixel(xx, yy, Color.Yellow) ' Color.FromArgb(rc, gc, bc))                                    
                '            End If
                '            yLine += 1
                '        Next
                '        Me.PictureBox1.Image = NonIndexedImage
                '        Me.PictureBox1.Refresh()

                '        xLine += 1
                '    Next

                'Next
                'Dim innerActiveblock = From ab In masterImageArray
                '                  Where (ab.xValue > arrayOfYCoordinates(yCoordCtr) + 1 AndAlso ab.xValue < arrayOfYCoordinates(yCoordCtr + 1) - 1) AndAlso
                '                  (ab.yValue > arrayOfXCoordinates(xCoordCtr) + 1 AndAlso ab.yValue < arrayOfXCoordinates(xCoordCtr + 1) - 1)
                'For Each item In innerActiveblock
                '    If item.dctValue >= TextBoxDCTCtr.Text Then
                '        xValue = item.yValue
                '        yValue = item.xValue

                '        Dim xLine As Integer = 0
                '        For xx = 0 + (xValue * 8) To 7 + (xValue * 8)
                '            Dim yLine As Integer = 0
                '            For yy = 0 + (yValue * 8) To 7 + (yValue * 8)
                '                If (xLine = 0 OrElse xLine = 7) OrElse (yLine = 0 OrElse yLine = 7) Then
                '                    NonIndexedImage.SetPixel(xx, yy, Color.Red) ' Color.FromArgb(rc, gc, bc))                                    
                '                End If
                '                yLine += 1
                '            Next
                '            Me.PictureBox1.Image = NonIndexedImage
                '            Me.PictureBox1.Refresh()

                '            xLine += 1
                '        Next
                '    End If
                'Next

            End If
        Next


        'ctrBlock = masterImageArray.Count - 1
        'For ctr = 0 To ctrBlock
        '    If arrayOfYCoordinates.Contains(masterImageArray(ctr).xValue) Or arrayOfXCoordinates.Contains(masterImageArray(ctr).yValue) Then
        '        xValue = masterImageArray(ctr).yValue
        '        yValue = masterImageArray(ctr).xValue

        '        Dim xLine As Integer = 0
        '        For xx = 0 + (xValue * 8) To 7 + (xValue * 8)
        '            Dim yLine As Integer = 0
        '            For yy = 0 + (yValue * 8) To 7 + (yValue * 8)
        '                If (xLine = 0 OrElse xLine = 7) OrElse (yLine = 0 OrElse yLine = 7) Then
        '                    NonIndexedImage.SetPixel(xx, yy, Color.Red) ' Color.FromArgb(rc, gc, bc))                                    
        '                End If
        '                yLine += 1
        '            Next
        '            Me.PictureBox1.Image = NonIndexedImage
        '            Me.PictureBox1.Refresh()

        '            xLine += 1
        '        Next

        '    End If
        'Next
        savedFileName = Path.GetDirectoryName(tbImage.Text) & "\xxx" & ofdLoadImage.SafeFileName
        NonIndexedImage.Save(savedFileName, System.Drawing.Imaging.ImageFormat.Jpeg)
    End Sub

    Private Sub btnReprocess_Click(sender As System.Object, e As System.EventArgs) Handles btnReprocess.Click
        processBlockDifferences()
    End Sub
    Sub SafeDispose(ByRef d As IDisposable)
        If Not d Is Nothing Then
            d.Dispose()
        End If
    End Sub
End Class